package mmi.gui.widget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import mmi.utils.Stats;

public class ExperimentFrame extends JFrame {
	private static final long serialVersionUID = -5967564881649736609L;

	private JPanel experimentDialogPanel = null;

	private JButton okButton = null;

	int nrTrials = 0;
	double totalTime = 0;

	Vector<Double> trialTimes = new Vector<Double>();
	Vector<Double> drawingTimes  = new Vector<Double>();
	Vector<Double> rotatingTimes  = new Vector<Double>();

	Vector<Double> movingTimes  = new Vector<Double>();


	Vector<Double> resizingTimes  = new Vector<Double>();



	JTextArea ta = new JTextArea();

	public ExperimentFrame()
	{
		super();

		experimentDialogPanel = new JPanel();
		getContentPane().add(experimentDialogPanel);

		//		experimentDialogPanel.add(new JLabel(getText()));
		experimentDialogPanel.add(ta);
		ta.setText(getText());

		okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		experimentDialogPanel.add(okButton);
		pack();
	}

	public void addTrialTime(Double time)
	{
		trialTimes.add(time);
		update();
	}

	/**
	 * @return the drawingTimes
	 */
	public Vector<Double> getDrawingTimes() {
		return drawingTimes;
	}



	/**
	 * @return the movingTimes
	 */
	public Vector<Double> getMovingTimes() {
		return movingTimes;
	}

	/**
	 * @return the rotatingTimes
	 */
	public Vector<Double> getResizingTimes() {
		return resizingTimes;
	}



	/**
	 * @return the rotatingTimes
	 */
	public Vector<Double> getRotatingTimes() {
		return rotatingTimes;
	}



	private String getText()
	{
		Stats<Double> drawingStats = new Stats<Double>(drawingTimes);
		Stats<Double> rotatingStats = new Stats<Double>(rotatingTimes);
		Stats<Double> movingStats = new Stats<Double>(movingTimes);
		Stats<Double> resizingStats = new Stats<Double>(resizingTimes);
		Stats<Double> trialStats = new Stats<Double>(trialTimes);

		return String.format("You have done %d trials.\n" +
				"You spent %d ms working on the trials.\n" +
				"It took you:\n" +
				"%d ms (s=%d, m=%d) to draw a shape\n" +
				"%d ms (s=%d, m=%d) to rotate;\n" +
				"%d ms (s=%d, m=%d) to move an object;\n" +
				"%d ms (s=%d, m=%d) to resize;\n",
				trialTimes.size(), (int) trialStats.sum(),
				(int) drawingStats.sum(),  (int) drawingStats.average(),  (int) drawingStats.standardDeviation(),
				(int) rotatingStats.sum(), (int) rotatingStats.average(), (int) rotatingStats.standardDeviation(),
				(int) movingStats.sum(),   (int) movingStats.average(),   (int) movingStats.standardDeviation(),
				(int) resizingStats.sum(), (int) resizingStats.average(), (int) resizingStats.standardDeviation()
		);

	}

	/**
	 * @return the totalTime
	 */
	public double getTotalTime() {
		return totalTime;
	}

	/**
	 * @param drawingTimes the drawingTimes to set
	 */
	public void setDrawingTimes(Vector<Double> drawingTimes) {
		this.drawingTimes = drawingTimes;
		update();
	}

	/**
	 * @param movingTimes the movingTimes to set
	 */
	public void setMovingTimes(Vector<Double> movingTimes) {
		this.movingTimes = movingTimes;
		update();
	}
	/**
	 * @param rotatingTimes the rotatingTimes to set
	 */
	public void setResizingTimes(Vector<Double> resizingTimes) {
		this.resizingTimes = resizingTimes;
		update();
	}
	/**
	 * @param rotatingTimes the rotatingTimes to set
	 */
	public void setRotatingTimes(Vector<Double> rotatingTimes) {
		this.rotatingTimes = rotatingTimes;
		update();
	}

	/**
	 * @param totalTime the totalTime to set
	 */
	public void setTotalTime(double totalTime) {
		this.totalTime = totalTime;
		update();
	}



	public void showFrame()
	{
		setVisible(true);
		repaint();
	}

	public void update()
	{
		ta.setText(getText());
		repaint();
	}

}
